#include "AffectsBIPHandle.h"
#include "PKB.h"

using namespace std;

AffectsBIPHandle::AffectsBIPHandle(void)
{
}

AffectsBIPHandle::~AffectsBIPHandle(void)
{
}
std::vector<std::vector<INTEGER>> AffectsBIPHandle::RetrieveAffectsBIP(
        std::vector<INTEGER> affecting_stmt, std::vector<INTEGER> affected_stmt)
{
        std::vector<std::vector<INTEGER>> result_vector;
        std::vector<INTEGER> affecting_out;
        std::vector<INTEGER> affected_out;

		std::set<INTEGER> temp;
		std::vector<INTEGER> found;
        unsigned int i,j;

		for (i = 0; i < affecting_stmt.size(); i++)
        {
			if (PKB::shortcutTable.GetStatementType(affecting_stmt[i]) == ASSIGN){
				STMT stmt;
				stmt.statement_number = affecting_stmt[i];
				stmt.node_type = ASSIGN;
			
				for (j = 0; j <affected_stmt.size();j++)
				{
					if (PKB::shortcutTable.GetStatementType(affected_stmt[j]) == ASSIGN){
						STMT stmt1;
						stmt1.statement_number = affected_stmt[j];
						stmt1.node_type = ASSIGN;
						if(PKB::affectsbip.GetAffectsBIP(stmt,stmt1)){
							affecting_out.push_back(affecting_stmt[i]);
							affected_out.push_back(affected_stmt[j]);
						}
					}
				}
			}
        }
        result_vector.push_back(affecting_out);
        result_vector.push_back(affected_out);
        
		return result_vector;
}
std::vector<std::vector<INTEGER>> AffectsBIPHandle::RetrieveTransitiveAffectsBIP(
	std::vector<INTEGER> affecting_stmt, std::vector<INTEGER> affected_stmt)
{
        std::vector<std::vector<INTEGER>> result_vector;
        std::vector<INTEGER> affecting_out;
        std::vector<INTEGER> affected_out;

		std::set<INTEGER> temp;
		std::vector<INTEGER> found;
        unsigned int i,j;

		for (i = 0; i < affecting_stmt.size(); i++)
        {
			if (PKB::shortcutTable.GetStatementType(affecting_stmt[i]) == ASSIGN){
				STMT stmt;
				stmt.statement_number = affecting_stmt[i];
				stmt.node_type = ASSIGN;
			
				for (j = 0; j <affected_stmt.size();j++)
				{
					if (PKB::shortcutTable.GetStatementType(affected_stmt[j]) == ASSIGN){
						STMT stmt1;
						stmt1.statement_number = affected_stmt[j];
						stmt1.node_type = ASSIGN;
						if(PKB::affectsbip.GetTransitiveAffectsBIP(stmt,stmt1)){
						
							affecting_out.push_back(affecting_stmt[i]);
							affected_out.push_back(affected_stmt[j]);
						}
					}
				}
			}
        }
        result_vector.push_back(affecting_out);
        result_vector.push_back(affected_out);
        
		return result_vector;
}
